package com.boarsoft.boar.config.var.biz;

import java.util.List;

import com.boarsoft.boar.config.entity.ConfigVar;
import org.hibernate.query.Query;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.boarsoft.boar.config.entity.ConfigVarApp;
import com.boarsoft.boar.config.var.ConfigVarAppBiz;
import com.boarsoft.boar.entity.AppInfo;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.hibernate.biz.SimpleBizImpl;

@Component("configVarAppBiz")
public class ConfigVarAppBizImpl extends SimpleBizImpl implements ConfigVarAppBiz {
    @Override
    @Transactional(readOnly = true)
    public List<AppInfo> list(String varId) {
        StringBuffer sb = new StringBuffer();
        sb.append(" from ").append(ConfigVarApp.class.getName());
        sb.append(" o join o.app a where o.varId='").append(varId).append("'");
        return dao.list("select a".concat(sb.toString()));
    }

    @Override
    @Transactional(readOnly = true)
    public PagedResult<AppInfo> list(String varId, String key, String orderBy, int pageNo, int pageSize) {
        StringBuffer sb = new StringBuffer();
        sb.append(" from ").append(ConfigVarApp.class.getName());
        sb.append(" o join o.app a where o.varId='").append(varId).append("'");
        if (Util.strIsNotEmpty(key)) {
            sb.append(" and (a.code like '%").append(key);
            sb.append("%' or a.name like '%").append(key);
            sb.append("%') ");
        }
        int total = Util.object2int(dao.getValue("select count(a.id)".concat(sb.toString())), 0);
        sb.append(" order by a.code");
        List<AppInfo> lt = dao.list("select a".concat(sb.toString()), pageNo, pageSize);
        return new PagedResult<AppInfo>(total, lt, pageNo, pageSize);
    }

    @Override
    @Transactional
    public void toggle(String varId, String[] appIds, boolean isAdd) {
        StringBuilder sb = new StringBuilder();
        sb.append("from ").append(ConfigVarApp.class.getName());
        sb.append(" where varId=? and appId=?");
        Query<ConfigVarApp> q = dao.createQuery(ConfigVarApp.class, sb.toString());
        for (String appId : appIds) {
            q.setParameter(0, varId);
            q.setParameter(1, appId);
            ConfigVarApp o = (ConfigVarApp) q.uniqueResult();
            if (o == null) {
                if (isAdd) {
                    o = new ConfigVarApp();
                    o.setAppId(appId);
                    o.setVarId(varId);
                    dao.save(o);
                }
            } else {
                dao.delete(o);
            }
        }
    }

    @Override
    @Transactional(readOnly = true)
    public String getProjIdByAppId(String appId) {
        StringBuilder sb = new StringBuilder();
        sb.append("from ").append(AppInfo.class.getName());
        sb.append(" where id= :id");
        Query<AppInfo> q = dao.createQuery(AppInfo.class, sb.toString());
        q.setParameter("id", appId);
        AppInfo appInfo = (AppInfo) q.uniqueResult();
        return appInfo.getProjId();
    }

    @Override
    @Transactional
    public void save(ConfigVarApp configVarApp) {

        ConfigVarApp o = dao.find(ConfigVarApp.class, String.format("and cVarId = '%s' and cAppId = '%s'", configVarApp.getVarId(), configVarApp.getAppId()));
        if(o==null){
            dao.save(configVarApp);
        }
    }
}